package utils

import (
	"crypto/rand"
	"fmt"
	"io"
	"strconv"
	"strings"
)

func NewUUID() string {
	uuid := make([]byte, 16)
	io.ReadFull(rand.Reader, uuid)
	uuid[8] = uuid[8]&^0xc0 | 0x80
	uuid[6] = uuid[6]&^0xf0 | 0x40
	return fmt.Sprintf("%X%X", uuid[0:5], uuid[8:13])
}

//func NewUUID() (string) {
//	uuid := make([]byte, 16)
//	io.ReadFull(rand.Reader, uuid)
//	uuid[8] = uuid[8]&^0xc0 | 0x80
//	uuid[6] = uuid[6]&^0xf0 | 0x40
//	return fmt.Sprintf("%X-%X-%X-%X-%X", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:14])
//}
func String2int(string string, def int) int {
	i, err := strconv.Atoi(string)
	if err != nil {
		return def
	}
	return i
}

//10进制转26进制
func Int32To26(n int) string {
	var s string
	for n > 0 {
		m := n % 26
		if m == 0 {
			m = 26
		}
		a := m + 64
		s = fmt.Sprintf("%c", a) + s
		n = (n - m) / 26
	}
	return s
}

//26进制转10进制
func I26ToInt32(s string) int {
	if len(s) <= 0 {
		return 0
	}
	var n uint8
	upper := strings.ToUpper(s)
	for i, j := len(upper)-1, 1; i >= 0; j *= 26 {
		c := upper[i]
		if c < 'A' || c > 'Z' {
			return 0
		}
		n += (c - 64) * uint8(j)
		i--
	}
	return int(n)

}
